package com.wyt.controller.role;

import com.wyt.dao.MenuMapper2;
import com.wyt.entity.Employee;
import com.wyt.until.CommonResult;
import com.wyt.until.MenuList6;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuMapper2 menuMapper2;
    @RequestMapping("findMenuByRoleId")
    public CommonResult findMenuByRoleId(HttpSession session){
        //获取登录者得信息
        Employee employee = (Employee) session.getAttribute("employee");
        Integer roleId = employee.geteRole();
        //根据角色查询该角色具有的菜单
        List<MenuList6> menuList6 = menuMapper2.findMenuByRoleId(roleId);
        //为菜单分层
        //查询所有的一级菜单。
        List<MenuList6> firstMenus=new ArrayList<>();
        for(MenuList6 Menu: menuList6){
            if(Menu.getMpid()==0){
                firstMenus.add(Menu);
            }
        }
        //2.找到一级菜单下所有子菜单
        for(MenuList6 first:firstMenus){
            //找到一级菜单下的所有子菜单
            List<MenuList6> children = findChildren(menuList6, first.getMid());
            first.setChildren(children);
        }
        //最多二级菜单 方法得递归调用。
        return new CommonResult(2000,"查询菜单成功",firstMenus);
    }
    private List<MenuList6> findChildren(List<MenuList6> menus, Integer pid){
        //子菜单
        List<MenuList6> children=new ArrayList<>();
        //遍历所有菜单
        for(MenuList6 m:menus){
            //得到指定pid得得子菜单
            if(m.getMpid().equals(pid)){
                children.add(m);
            }
        }
        //遍历pid下的所有子菜单-- 方法得递归调用
        for(MenuList6 c:children){
            c.setChildren(findChildren(menus,c.getMid()));
        }
        return children;
    }

    //获取所有的菜单
    @RequestMapping("findAllMenu")
    public CommonResult findAll(){
        List<MenuList6> all= menuMapper2.selectAll();
        //找到所有的一级菜单
        List<MenuList6> first=new ArrayList<>();
        for(MenuList6 m:all){
            if(m.getMpid()==0){
                first.add(m);
            }
        }
        //设置子菜单
        for(MenuList6 f:first){
            f.setChildren(findChildren(all,f.getMid()));
        }
        return new CommonResult(2000,"查询成功",first);
    }

    @RequestMapping("getMenuIds")
    public CommonResult getMenuIds(Integer rid){
        List<Integer> mids= menuMapper2.getMenuIds(rid);
        return new CommonResult(2000,"查询成功",mids);
    }

    @RequestMapping("fenMenu")
    public CommonResult fenMenu(Integer[] mIds,Integer rid){
        //1.删除原来角色具有的权限
        menuMapper2.deleteByRid(rid);
        //2.添加新的权限
        for(Integer mid:mIds){
            menuMapper2.insert(rid,mid);
        }
        return new CommonResult(2000,"分配成功",null);
    }
    @RequestMapping("getAllMidByRid")
    private CommonResult getAllMidByRid(List mids){
        menuMapper2.getAllMidByRid(mids);
        return new CommonResult(0,"",null);
    }
}
